<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/13
 * Time: 10:21
 */


class ActionApi extends Base
{
    public function generate()
    {
        $province = $this->input('province');
        $city     = $this->input('city');
        $area     = $this->input('area');
        $year     = $this->input('year');
        $month    = $this->input('month');
        $day      = $this->input('day');
        $sex      = $this->input('sex');
        $number   = $this->input('number');
        if (stripos($province, '请选择') !== false) {
            return $this->setJson(100, '请选择省份');
        }
        if (stripos($city, '请选择') !== false) {
            return $this->setJson(101, '请选择城市');
        }
        if (stripos($area, '请选择') !== false) {
            return $this->setJson(102, '请选择区域');
        }
        if (stripos($year, '请选择') !== false) {
            return $this->setJson(103, '请选择年份');
        }
        if (stripos($month, '请选择') !== false) {
            return $this->setJson(104, '请选择月份');
        }
        if (stripos($day, '请选择') !== false) {
            return $this->setJson(105, '请选择日期');
        }
        if ($sex == '') {
            return $this->setJson(106, '请选择性别');
        }
        if (intval($number) <= 0) {
            return $this->setJson(107, '数字非法');
        }
        $special = array('北京', '上海', '天津');
        $addr    = '';
        if (in_array($province, $special)) {
            $province .= '市';
            $addr     = $province . $area;
        } else {
            $addr = $city . $area;
        }
        $code = $this->search($addr);
        if (!$code) {
            $code = $this->search($area);
        }
        if (!$code) {
            return $this->setJson(108, '没有找到此地区的代码');
        }
        $card14    = $code . $year . $month . $day;
        $card_list = array();
        for ($i = 0; $i < $number; $i++) {
            $card_list[] = $this->createCard($card14, $sex);
        }
        return $this->setJson(0, '生成OK', $card_list);
    }

    public function check_card()
    {
        $card = $this->input('card');
        if (empty($card) || strlen($card) != 18) {
            return $this->setJson(10, '请输入18位号码');
        }
        $checkGene = $this->checkGene($card);
        if (!$checkGene) {
            return $this->setJson(11, '非法身份证,不合法');
        }
        $char6        = substr($card, 0, 6);
        $birth_format = '%s年%s月%s日';
        $birth_format = sprintf($birth_format, substr($card, 6, 4), substr($card, 10, 2), substr($card, 12, 2));
        $sex          = intval(substr($card, -4, 3)) % 2 == 0 ? '女性' : '男性';
        $address      = $this->getCardAddress($char6);
        $result       = array();
        $result[]     = '归属地：' . $address;
        $result[]     = '出生日期：' . $birth_format;
        $result[]     = '性别：' . $sex;//倒数第4的3位
        $result[]     = '查询时间：' . date('Y-m-d H:i:s');
        return $this->setJson(0, 'OK', $result);
    }

    public function loadCard1()
    {
        $char = 12312312312;
        $char = strval($char);
        for ($i = 0; $i < strlen($char); $i++) {
            $this->dump($char[$i]);
        }
        $this->dump(self::loadCard());
    }
}

class Base
{
    /**
     * 生成完整的身份证号
     * @param $char14
     * @param $sex
     * @return string
     */
    public function createCard($char14, $sex)
    {
        //权重号
        $weight = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        //因子
        $gene   = array(
            0  => 1,
            1  => 0,
            2  => 'X',
            3  => 9,
            4  => 8,
            5  => 7,
            6  => 6,
            7  => 5,
            8  => 4,
            9  => 3,
            10 => 2,
        );
        $char3  = $this->create3Char($sex);
        $char17 = strval($char14 . $char3);
        $sum    = 0;
        for ($i = 0; $i < strlen($char17); $i++) {
            $sum += ($char17[$i] * $weight[$i]);
        }
        $lastIndex = $sum % 11;
        return $char17 . $gene[$lastIndex];
    }

    /**
     * 检查最后的校正码
     * @param $card18
     * @return bool
     */
    public function checkGene($card18)
    {
        //权重号
        $weight = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        //因子
        $gene      = array(
            0  => 1,
            1  => 0,
            2  => 'X',
            3  => 9,
            4  => 8,
            5  => 7,
            6  => 6,
            7  => 5,
            8  => 4,
            9  => 3,
            10 => 2,
        );
        $sum       = 0;
        $card17    = substr($card18, 0, 17);
        $checkGene = substr($card18, -1, 1);
        for ($i = 0; $i < strlen($card17); $i++) {
            $sum += ($card17[$i] * $weight[$i]);
        }
        $lastIndex = $sum % 11;
        $gene      = $gene[$lastIndex];
        if ($gene == $checkGene) {
            return true;
        }
        return false;
    }

    /**
     * 随机后后三位
     * @param $sex int 0代表女,1代表男
     * @return int
     */
    public function create3Char($sex)
    {
        while (true) {
            $char = mt_rand(100, 999);
            //偶数分给女性
            if ($sex == 1) {
                if ($char % 2 != 0) {
                    return $char;
                }
            } else {
                if ($char % 2 == 0) {
                    return $char;
                }
            }
        }
    }

    /**
     * 获取地区
     * @param $char6
     * @return bool|mixed
     */
    public function getCardAddress($char6)
    {
        $list = self::loadCard();
        if (isset($list[$char6])) {
            return $list[$char6];
        }
        return false;
    }

    /**
     * 加载身份证号
     * @return array
     */
    public static function loadCard()
    {
        $data = include('card.php');
        $data = array_map('trim', explode("\n", $data));
        $list = array();
        foreach ($data as $val) {
            $tmp           = preg_replace('/\s+/', '|', $val);
            $tmp           = explode('|', $tmp);
            $list[$tmp[0]] = $tmp[1];
        }
        return $list;
    }

    public function search($search)
    {
        $data = self::loadCard();
        foreach ($data as $code => $address) {
            if (stripos($address, $search) !== false) {
                return $code;
            }
        }
        return false;
    }


    public function input($field, $default = '')
    {
        return isset($_REQUEST[$field]) ? trim($_REQUEST[$field]) : $default;
    }

    public static function load()
    {
        return new self();
    }

    public function log($content, $flag)
    {
        ob_start();
        echo date('Y-m-d H:i:s') . '---------------------' . $flag . '-----------------------------' . "\n";
        var_dump($content);
        echo "\n";
        $content = ob_get_contents();
        ob_end_clean();
        return file_put_contents('debug.txt', $content, 8);
    }

    /**
     * 设置返回json
     * @param $status
     * @param $msg
     * @param null $data
     * @return string
     */
    public function setJson($status, $msg, $data = null)
    {
        return json_encode(self::setRs($status, $msg, $data));
    }

    /**
     * 设置返回结果
     * @param $status
     * @param $msg
     * @param null $data
     * @return array
     */
    public function setRs($status, $msg, $data = null)
    {
        $content = array(
            'status' => $status,
            'msg'    => $msg,
        );
        if (!is_null($data)) {
            $content['data'] = $data;
        }
        return $content;
    }

    public function dump()
    {
        $args = func_get_args();
        ob_start();
        echo '<pre>';
        foreach ($args as $val) {
            var_dump($val);
        }
        echo '</pre>';
        $content = ob_get_contents();
        ob_end_clean();
        echo $content;
    }
}

$act  = isset($_GET['act']) ? trim($_GET['act']) : '';
$obj  = new ActionApi();
$bool = method_exists($obj, $act);
Base::load()->log([$act, $obj], 'check_method2');
if (!method_exists($obj, $act)) {
    exit(Base::setJson(404, $act . '方法不存在'));
}
$result = call_user_func(array($obj, $act));
if (is_array($result)) {
    exit(json_encode($result));
} else {
    exit($result);
}